import java.util.ArrayList;
import java.util.List;
public class Hall {
    private String hallName;
    private int totalRows;
    private int seatsPerRow;
    private List<Seat> seats;

    public Hall(String hallName, int totalRows, int seatsPerRow) {
        this.hallName = hallName;
        this.totalRows = totalRows;
        this.seatsPerRow = seatsPerRow;
        this.seats = new ArrayList<>();
        resetSeats();
    }

    public void resetSeats() {
        seats.clear();
        for (int row = 1; row <= totalRows; row++) {
            for (int col = 1; col <= seatsPerRow; col++) {
                seats.add(new Seat(row, col, ""));
            }
        }
    }

    public void printSeatMap() {
        System.out.print("   ");
        for (int col = 1; col <= seatsPerRow; col++) {
            System.out.printf("%2d", col);
        }
        System.out.println();

        for (int row = 1; row <= totalRows; row++) {
            System.out.printf("%2d ", row);
            for (int col = 1; col <= seatsPerRow; col++) {
                Seat seat = getSeat(row, col);
                System.out.print(seat.isSold() ? " X" : " O");
            }
            System.out.println();
        }
    }
    public Seat getSeat(int row, int col) {
        for (Seat seat : seats) {
            if (seat.getRow() == row && seat.getCol() == col) {
                return seat;
            }
        }
        return null;
    }
    public void initSeatsForShow(String showId) {
        if (seats == null) {
            seats = new ArrayList<>();
            for (int row = 1; row <= totalRows; row++) {
                for (int col = 1; col <= seatsPerRow; col++) {
                    seats.add(new Seat(row, col, showId));
                }
            }
        } else {
            for (Seat seat : seats) {
                seat.reset(showId);
            }
        }
        Log.info("系统", "初始化座位", "影厅：" + hallName + "，场次ID：" + showId + "，座位数：" + getTotalSeatsCount());
    }
    public int getTotalSeatsCount() {
        return totalRows * seatsPerRow;
    }

    public int getAvailableSeatsCount() {
        int count = 0;
        for (Seat seat : seats) {
            if (!seat.isSold()) {
                count++;
            }
        }
        return count;
    }
    public String getHallName() {
        return hallName;
    }
    public int getTotalRows() {
        return totalRows;
    }
    public void setTotalRows(int totalRows) {
        this.totalRows = totalRows;
    }
    public int getSeatsPerRow() {
        return seatsPerRow;
    }
    public void setSeatsPerRow(int seatsPerRow) {
        this.seatsPerRow = seatsPerRow;
    }
    public List<Seat> getSeats() {
        return seats;
    }
}